﻿using FB.Mvc.Controllers;
using FCDB.Entity;
using FCDB.Entity.EntityEx;
using FCDB.Repository;
using FCDB.Web.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wf5.Engine.Business;
using Wf5.Engine.Common;

namespace FCDB.Web.Controllers
{
       [FB.Membership.Annotations.AuthorizeOnly]
    public class ProcessQueryController : BaseController
    {
        //
        // GET: /ProcessQuery/
        private WfProcessInstanceRepository Repository = new WfProcessInstanceRepository();
        private WfProcessRepository ProcessRepository = new WfProcessRepository();
        private IWorkflowService service = new WorkflowService();
        private WfActivityInstanceTasksRepository ActiveInstanceTaskRepository = new WfActivityInstanceTasksRepository();
        public ActionResult Index()
        {
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                string dtStart = Request["dtStart"];
                string dtEnd = Request["dtEnd"];
                string kewyword = Request["keyword"];
                string UserIDs = Request["UserIDs"];
                string DeparmentID = Request["DeparmentID"];
                string ProcessGuid = Request["ProcessGUID"]; 
                int? ProcessStatus = String.IsNullOrEmpty(Request["ProcessStatus"])?0:int.Parse(Request["ProcessStatus"]);
                IList<WfProcessInstanceEx> list = Repository.GetAllProccessInstance(this.UserID, 0, skip, take,ProcessStatus, out total, "", 0, dtStart
                    , dtEnd, kewyword, UserIDs, DeparmentID, ProcessGuid);
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json;

            }
            WFWorkFlowRepository WFRespository = new WFWorkFlowRepository();
            IList<WfProcess> processList = WFRespository.GetAllProcess();
            ViewBag.ProcessList = processList;
            return View(); 
        }

       
        public ActionResult Transiting()
        {
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                string dtStart = Request["dtStart"];
                string dtEnd = Request["dtEnd"];
                string kewyword = Request["keyword"];
                string UserIDs = Request["UserIDs"];
                string DeparmentID = Request["DeparmentID"];
                string ProcessGuid = Request["ProcessGUID"];
                IList<WfProcessInstanceEx> list = Repository.GetAllProccessInstance(this.UserID, (short)ProcessStateEnum.Running, skip, take, out total, "", 0, dtStart
                    , dtEnd, kewyword, UserIDs, DeparmentID, ProcessGuid);
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json;

            }
            WFWorkFlowRepository WFRespository = new WFWorkFlowRepository();
            IList<WfProcess> processList = WFRespository.GetAllProcess();
            ViewBag.ProcessList = processList;
            return View(); 
        }
        public ActionResult Completed()
        {
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                string dtStart = Request["dtStart"];
                string dtEnd = Request["dtEnd"];
                string kewyword = Request["keyword"];
                string UserIDs = Request["UserIDs"];
                string DeparmentID = Request["DeparmentID"];
                string ProcessGuid = Request["ProcessGUID"];
                IList<WfProcessInstanceEx> list = Repository.GetAllProccessInstance(this.UserID, (short)ProcessStateEnum.Completed, skip, take, out total, "", 0, dtStart
                    , dtEnd, kewyword, UserIDs, DeparmentID, ProcessGuid);
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json; 
            }
            WFWorkFlowRepository WFRespository = new WFWorkFlowRepository();
            IList<WfProcess> processList = WFRespository.GetAllProcess();
            ViewBag.ProcessList = processList;
            return View(); 
        }
        public ActionResult Checked()
        {
            return View();
        }

        public ActionResult ViewProccessInstance()
        {
            if (string.IsNullOrEmpty(Request["AppInstanceID"])) return Content("没有存在该表单");
            string AppInstanceID = Request["AppInstanceID"];
            string AppName = Request["AppName"];
            string ProcessGUID = Request["ProcessGUID"];
            WfProcessInstance instance = service.GetProcessInstance(int.Parse(AppInstanceID));
            WfProcess process = ProcessRepository.GetById(instance.ProcessGUID);
            string url = string.Format("~/{0}?ProcessGuid={1}&AppInstanceID={2}&&ViewDetail=ViewDetail", process.PageUrl, process.ProcessGUID, instance.AppInstanceID);
            return Redirect(url);
         }
    }
}
